package com.sky.controller.user;

import com.sky.controller.BaseController;
import com.sky.enumeration.DishStatusEnum;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = "C端菜品模块")
@RestController("UserDishController")
@RequestMapping("/user/dish")
@Slf4j
public class DishController extends BaseController {

    @Autowired
    private DishService dishService;

    @ApiOperation("根据分类id查询菜品")
    @GetMapping("/list")
    @Cacheable(cacheNames = "dishCategoryIdCache", key = "#categoryId")
    public Result list(@RequestParam Integer categoryId) {
        log.info("根据分类id查询菜品 =>{}", categoryId);
//        // 获取缓存中的数据
//        String key = "dish_" + categoryId;
//        Object cache = redisCacheUtils.getCacheObject(key);
//        if (cache != null) {
//            JSONArray objects = JSONArray.parseArray((String) cache);
//            log.info("读取缓存数据 =>{}", objects);
//            return Result.success(objects);
//        }

        DishVO dishVO = new DishVO();
        dishVO.setStatus(DishStatusEnum.Enable);
        dishVO.setCategoryId(Long.valueOf(categoryId));
        List<DishVO> list = dishService.getByCategoryIdWithUser(dishVO);
//        String jsonString = JSONObject.toJSONString(list);
//        redisCacheUtils.setCacheObject(key, jsonString);
        return Result.success(list);
    }
}
